package org.example.route.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.extern.slf4j.Slf4j;
import org.example.route.interceptor.JwtTokenUserInterceptor;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {

    private final JwtTokenUserInterceptor jwtTokenUserInterceptor;

    @Autowired
    public WebConfig(JwtTokenUserInterceptor jwtTokenUserInterceptor) {
        this.jwtTokenUserInterceptor = jwtTokenUserInterceptor;
    }

    // 1. 拦截器配置（补全Swagger3路径排除）
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/user/login", "/user/register", "/upload/avatar",
                        // Swagger3/Knife4j核心路径 ↓
                        "/doc.html", "/webjars/**",
                        "/swagger-resources/**", "/v3/api-docs/**",
                        "/swagger-ui/**", "/swagger-ui.html", "/swagger-ui/index.html",  // 新增
                        "/v3/api-docs",            // 新增基础端点
                        "/swagger-ui/swagger-initializer.js" // 避免 JS 被拦截  // 新增Swagger UI路径
                );
    }

    // 2. 替换Docket为OpenAPI配置
    @Bean
    public OpenAPI customOpenAPI() {

        return new OpenAPI()
                .components(new Components()
                        .addSecuritySchemes("PureToken",
                                new SecurityScheme()
                                        .type(SecurityScheme.Type.APIKEY)
                                        .in(SecurityScheme.In.HEADER)
                                        .name("Authorization")  // 参数名固定为Authorization
                                        .description("直接输入Token，无需Bearer前缀")  // 明确提示用户
                        )
                )
                .addSecurityItem(new SecurityRequirement().addList("PureToken"));
    }



    // 3. 接口分组配置（替代原Docket扫描规则）
    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("route-system")  // 自定义分组名
                .packagesToScan("org.example.route.controller")  // 指定扫描包
                .pathsToMatch("/**")    // 匹配所有路径
                .build();
    }

    // 4. 更新资源映射（关键修改）
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // SpringDoc Swagger UI 资源映射
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.18.2/");

        // WebJars 资源映射
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
